function typ = getColumnType(col, tab, DBobj, all)
  % Get datatype of column col in table tab from database DBobj
  % If all==true (not the default) typ is a cell array of all distinct column types in tab
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Release date: 9/15/2011

    global lfcr
    
    if isempty(lfcr)
        lfcr = char(10);
    end
  
    if nargin < 4
        all = false;
    end
  
    if isconnection(DBobj)
        if all
            query = ['SELECT name FROM sys.types WHERE system_type_id IN '... 
                     '(SELECT DISTINCT system_type_id FROM sys.columns c '...
                     'INNER JOIN sys.tables t ON c.object_id = t.object_id '...
                     'WHERE t.name=''' tab ''')'];
        else
            query = ['SELECT name FROM sys.types WHERE system_type_id IN '... 
                     '(SELECT DISTINCT system_type_id FROM sys.columns c '...
                     'INNER JOIN sys.tables t ON c.object_id = t.object_id '...
                     'WHERE t.name=''' tab ''' AND c.name=''' col ''')'];
        end
        try
            curs = fetch(exec(DBobj, query));
            if isempty(curs.Message)
                if numel(curs.Data)==1 
                    typ = curs.Data{:};
                else
                    typ = curs.Data;
                end
            else
                typ = ['Query to determine the datatype of ' col ' returned the following error:'...
                       lfcr curs.Message]; 
            end
        catch me
                typ = ['Attempt to determine the datatype of ' col ' resulted in a runtime exception.'...
                       lfcr 'Here is the exception report:' lfcr getReport(me)]; 
        end
    else
        typ = ['Sorry, connection to database ' DBobj.Instance ' lost.  Attempt to determine the '...
               'datatype of ' col ' failed.']; 
    end